﻿using Hx.ADSyncPlatform.ActiveDirectory.Enums;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Reflection;

namespace Hx.ADSyncPlatform.ActiveDirectory.Extension
{
    public static class PropertyCollectionConvertExtension
    {
        internal static T ConvertTo<T>(this ResultPropertyCollection result, List<string> propertiesToLoad = null)
        {
            T obj = default(T);
            if (result != null)
            {
                obj = Activator.CreateInstance<T>();
                foreach (var propertyName in result.PropertyNames)
                {

                    try
                    {
                        BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
                        PropertyInfo prop = obj.GetType().GetProperty((string)propertyName, flag);

                        //先不使用注解
                        //var columnMapping= prop.GetCustomAttribute<ColumnMappingAttribute>();
                        //string propName = "";
                        //if (columnMapping != null)
                        //{
                        //    if(columnMapping.)
                        //}
                        object value = null;
                        if (((string)propertyName)?.ToLower() == "AccountExpires".ToLower())
                        {
                            long accountExpires = (long)result[(string)propertyName][0];
                            if (accountExpires > 0)
                            {
                                value = new DateTime(1601, 01, 01, 0, 0, 0, DateTimeKind.Utc).AddTicks(accountExpires);
                            }

                        }
                        else if (prop != null && prop.PropertyType == typeof(List<string>))
                        {
                            List<string> vs = new List<string>();
                            for (int i = 0; i < result[(string)propertyName].Count; i++)
                            {
                                vs.Add((string)result[(string)propertyName][i]);
                            }
                            value = vs;
                        }
                        else if (prop != null && prop.PropertyType == typeof(GroupType))
                        {
                            value = (GroupType)result[(string)propertyName][0];
                        }
                        else
                        {
                            value = result[(string)propertyName][0];
                        }

                        if (prop != null)
                        {
                            prop.SetValue(obj, value);
                        }
                    }
                    catch
                    {
                        //hack:异常处理
                        //异常处理
                    }

                }
            }
            return obj;
        }

        internal static T ConvertTo<T>(this PropertyCollection result, List<string> propertiesToLoad = null)
        {
            T obj = default(T);
            if (result != null)
            {
                obj = Activator.CreateInstance<T>();
                foreach (var propertyName in result.PropertyNames)
                {

                    try
                    {
                        BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
                        PropertyInfo prop = obj.GetType().GetProperty((string)propertyName, flag);

                        //先不使用注解
                        //var columnMapping= prop.GetCustomAttribute<ColumnMappingAttribute>();
                        //string propName = "";
                        //if (columnMapping != null)
                        //{
                        //    if(columnMapping.)
                        //}
                        object value = null;
                        if (((string)propertyName)?.ToLower() == "AccountExpires".ToLower())
                        {
                            long accountExpires = (long)result[(string)propertyName][0];
                            if (accountExpires > 0)
                            {
                                value = new DateTime(1601, 01, 01, 0, 0, 0, DateTimeKind.Utc).AddTicks(accountExpires);
                            }

                        }
                        else if (prop != null && prop.PropertyType == typeof(List<string>))
                        {
                            List<string> vs = new List<string>();
                            for (int i = 0; i < result[(string)propertyName].Count; i++)
                            {
                                vs.Add((string)result[(string)propertyName][i]);
                            }
                            value = vs;
                        }
                        else if (prop != null && prop.PropertyType == typeof(GroupType))
                        {
                            value = (GroupType)result[(string)propertyName][0];
                        }
                        else
                        {
                            value = result[(string)propertyName][0];
                        }

                        if (prop != null)
                        {
                            prop.SetValue(obj, value);
                        }
                    }
                    catch
                    {
                        //hack:异常处理
                        //异常处理
                    }

                }
            }
            return obj;
        }

    }
}
